package in.muton.edushare.components.alertpanel;

import in.muton.edushare.components.alertpanel.api.AlertMgr;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.DateComparision;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.ScheduleNotifications;
import com.edushare.common.vo.Schedules;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserAssignmentLink;
import com.edushare.common.vo.UserPoll;
import com.edushare.common.vo.Users;

public class AlertPanel extends EdusharePanel {

	private static final Log log = LogFactory.getLog(AlertPanel.class);
	
	public AlertPanel(String id, final IModel<?> model) {
		super(id);
		objModel = model;
		final WebMarkupContainer wmc = new WebMarkupContainer("wmc");
		add(wmc.setOutputMarkupPlaceholderTag(true));

		wmc.add(new ListView<IModel<DateComparision>>("alertList",
				new InnerList<DateComparision>() {
					private static final long serialVersionUID = 6592317580731891813L;

					@Override
					public List<IModel<DateComparision>> getObject() {
						List<DateComparision> idc;
						if (model.getObject() instanceof Users)
							idc = new AlertMgr((Users) model.getObject())
									.getEvents();
						else {
							int creatorType = 0;
							int creatorId = 0;
							if (model.getObject() instanceof Universities) {
								creatorType = Messages.RECEIVER_TYPE_UNIVERSITY;
								creatorId = ((Universities) model.getObject())
										.getUniversityId();
							} else if (model.getObject() instanceof Departments) {
								creatorType = Messages.RECEIVER_TYPE_DEPARTMENT;
								creatorId = ((Departments) model.getObject())
										.getDepartmentId();
							} else if (model.getObject() instanceof CourseInstances) {
								creatorType = Messages.RECEIVER_TYPE_COURSE;
								creatorId = ((CourseInstances) model
										.getObject()).getCourseInstanceId();
							}
							idc = AlertMgr.getEventsByGroup(model, creatorType,
									creatorId);
						}
						log.debug("IDC size = "+idc.size());
						if (all || idc.size() < 8)
							return new CayenneList<DateComparision>(idc)
									.getObject();
						else
							return new CayenneList<DateComparision>(idc
									.subList(0, 7)).getObject();
					}
				}) {
			private static final long serialVersionUID = 3440702900875689034L;

			@Override
			protected void populateItem(ListItem<IModel<DateComparision>> arg0) {
				IModel<DateComparision> item = arg0.getModelObject();
				if (item.getObject() instanceof ScheduleNotifications
						|| item.getObject() instanceof Schedules) {
					arg0.add(new SchAlert("alert", item));
				} else if (item.getObject() instanceof UserPoll) {
					UserPoll p = (UserPoll) item.getObject();
					arg0.add(new PollAlert("alert", p));
				} else if (item.getObject() instanceof Poll) {
					Poll p = (Poll) item.getObject();
					arg0.add(new PollAlert("alert", p));
				} else if (item.getObject() instanceof UserAssignmentLink) {
					UserAssignmentLink ul = (UserAssignmentLink) item
							.getObject();
					arg0.add(new AssgnAlert("alert", ul));
				} else if (item.getObject() instanceof Assignments) {
					Assignments as = (Assignments) item.getObject();
					arg0.add(new AssgnAlert("alert", as));
				} else
					arg0.add(new EmptyPanel("alert"));
			}
		});

		AjaxLink<Void> seeAll = new AjaxLink<Void>("seeAll") {
			private static final long serialVersionUID = 3168175353774675305L;

			@Override
			public void onClick(AjaxRequestTarget arg0) {
				all = true;
				arg0.addComponent(wmc);
				List<ScheduleNotifications> sl = getLoggedInUser().getSchedules();
				for(ScheduleNotifications s:sl)
					s.setActionStatus(ScheduleNotifications.OPEN);
				getORMInterface().commitAllChanges();
			}
		};
		
		Label alertTxt = null;
		if(objModel.getObject() instanceof Users){
			alertTxt = new Label("alertTxt", "see all alerts");
		}else{
			alertTxt = new Label("alertTxt", "see all events");
		}
		seeAll.add(alertTxt.setRenderBodyOnly(true));
		add(seeAll);
	}

	@Override
	public boolean isVisible() {
		if(!(objModel.getObject() instanceof Users))
			return true;
		if(getLoggedInUser() == (Users)objModel.getObject())
			return true;
		return false;
	}

	IModel<?> objModel ;
	private boolean all = false;
	private static final long serialVersionUID = 4176826035403975932L;
}
